﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text;

namespace MIT.DivAdmin
{
    public partial class DivAdminSecondInterview : System.Web.UI.Page
    {
        String email;
        String name;
        String lastname;

        String Req1;
        String Req2;
        String Req3;
        String NewsDate1;
        String NewsDate2;
        String NewsDate3;
        String NewsTitle1;
        String NewsTitle2;
        String NewsTitle3;
        String NewsDetails1;
        String NewsDetails2;
        String NewsDetails3;
        protected void Page_Load(object sender, EventArgs e)
        {
            String Name = (String)Session["LoggingInfo"];
            String Position = (String)Session["LoggingPosition"];
            String username = (String)Session["LoggingInfoUsername"];
            LabelDivAdminName.Text = Name;
            LabelDivAdminPosition.Text = "(" + Position + ")";

            ImageProfile.ImageUrl = "../ProfilePicHandler.ashx?uname=" + username;

            Session["CalLoggingInfo"] = Name;
            Session["CalLoggingPosition"] = Position;
            Session["CalLoggingInfoUsername"] = username;

            ///////////////////Notification Number//////////////////////////////////////////////////
            string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand ReqMsgNo = new SqlCommand("spMsgCounter", con);
                ReqMsgNo.CommandType = CommandType.StoredProcedure;

                SqlParameter ReqName3 = new SqlParameter("@username", username);
                ReqMsgNo.Parameters.Add(ReqName3);



                con.Open();
                SqlDataReader dr;

                dr = ReqMsgNo.ExecuteReader();

                if (dr.Read())
                {
                    Req1 = Convert.ToString(dr["REQUEST1"]);
                    Req2 = Convert.ToString(dr["REQUEST2"]);
                    Req3 = Convert.ToString(dr["REQUEST3"]);
                }

            }
            if (Req1 == "")
            {
                LabelMsgNo.Text = "";

            }
            else if (Req2 == "")
            {
                LabelMsgNo.Text = "1";
            }
            else if (Req3 == "")
            {
                LabelMsgNo.Text = "2";
            }
            else
            {
                LabelMsgNo.Text = "3";
            }


            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews1", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News1 = new SqlParameter("@News1", 1);
                GetNews.Parameters.Add(News1);



                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate1 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle1 = Convert.ToString(dr["Title"]);
                    NewsDetails1 = Convert.ToString(dr["Details"]);

                }

            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews2", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News2 = new SqlParameter("@News2", 2);
                GetNews.Parameters.Add(News2);



                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate2 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle2 = Convert.ToString(dr["Title"]);
                    NewsDetails2 = Convert.ToString(dr["Details"]);

                }

            }

            using (SqlConnection con = new SqlConnection(CS))
            {
                SqlCommand GetNews = new SqlCommand("spGetNews3", con);
                GetNews.CommandType = CommandType.StoredProcedure;

                SqlParameter News3 = new SqlParameter("@News3", 3);
                GetNews.Parameters.Add(News3);



                con.Open();
                SqlDataReader dr;

                dr = GetNews.ExecuteReader();

                if (dr.Read())
                {
                    NewsDate3 = Convert.ToString(dr["LiveDate"]);
                    NewsTitle3 = Convert.ToString(dr["Title"]);
                    NewsDetails3 = Convert.ToString(dr["Details"]);
                }

            }



            LabelDetails1.Text = NewsDetails1;
            LabelDetails2.Text = NewsDetails2;
            LabelDetails3.Text = NewsDetails3;

            LabelNews1.Text = NewsTitle1;
            LabelNews2.Text = NewsTitle2;
            LabelNews3.Text = NewsTitle3;

            LabelDate1.Text = NewsDate1;
            LabelDate2.Text = NewsDate2;
            LabelDate3.Text = NewsDate3;
        }

        protected void ButtonLogout_Click(object sender, EventArgs e)
        {
            Response.Redirect("../Login/SignIn.aspx");
        }
        protected void ButtonInviteCandidates_Click(object sender, EventArgs e)
        {
            int InterviewNo = (int)Session["InterviewNo"];
            String InterviewDate = (String)Session["InterviewDateInfo"];
            String BoardRoom = (String)Session["RoomCode"];
            String Start = (String)Session["Start"];
            String End = (String)Session["End"];

            StringBuilder str = new StringBuilder();

            // Select the checkboxes from the GridView control
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                GridViewRow row = GridView1.Rows[i];
                bool isChecked = ((CheckBox)row.FindControl("checkSelect")).Checked;

                if (isChecked)
                {
                    int a = Convert.ToInt32(GridView1.Rows[i].Cells[1].Text);

                    string CS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

                    using (SqlConnection con = new SqlConnection(CS))
                    {

                        SqlCommand com = new SqlCommand("spGetInterviweeEmail", con);
                        com.CommandType = CommandType.StoredProcedure;

                        SqlParameter getemail = new SqlParameter("@GetEmail", a);
                        com.Parameters.Add(getemail);



                        con.Open();
                        SqlDataReader dr;

                        dr = com.ExecuteReader();

                        if (dr.Read())
                        {
                            email = dr["EMAIL"].ToString();
                            name = dr["FIRSTNAME"].ToString();
                            lastname = dr["LASTNAME"].ToString();
                        }
                        con.Close();

                    }



                    if (Page.IsValid)
                        try
                        {
                            MailMessage mm = new MailMessage("cms.mit2015@gmail.com", email);
                            //mm.From = new MailAddress();
                            //mm.To.Add();
                            mm.Subject = "MIT Interview Invitation";

                            mm.Body = "Hi " + name + ",/nCongratulations!.You are being selected for our company's 1st Interview.\n\nInterview No:" + InterviewNo + "\nDate:" + InterviewDate + "\nVenue:" + BoardRoom + ".";

                            mm.IsBodyHtml = true;

                            SmtpClient smtpClient = new SmtpClient("smtp.gmail.com", 587);
                            smtpClient.EnableSsl = true;
                            smtpClient.Credentials = new System.Net.NetworkCredential()
                            {
                                UserName = "cms.mit2015@gmail.com",
                                Password = "millenium2015"
                            };
                            smtpClient.Send(mm);


                        }

                        catch (Exception ex)
                        {
                            Response.Write("Could not send the e-mail - error: " + ex.Message);
                        }



                }
                string CSS = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;


                using (SqlConnection con = new SqlConnection(CSS))
                {
                    SqlCommand cmd = new SqlCommand("spAddToInterviewOne", con);
                    cmd.CommandType = System.Data.CommandType.StoredProcedure;

                    cmd.Parameters.AddWithValue("@Name", name);
                    cmd.Parameters.AddWithValue("@LName", lastname);
                    cmd.Parameters.AddWithValue("@InterviewNo", InterviewNo);
                    cmd.Parameters.AddWithValue("@InterviewDate", InterviewDate);
                    cmd.Parameters.AddWithValue("@Start", Start);
                    cmd.Parameters.AddWithValue("@End", End);
                    cmd.Parameters.AddWithValue("@BoardRoom", BoardRoom);




                    con.Open();
                    cmd.ExecuteNonQuery();


                }
            }
            Response.Redirect("DivAdminTools.aspx");
        }
    }
}